package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


/**
 * 目标:
 *
 * @author YUE
 * @date 2024/6/23 18:35
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /*
       查看今日运营数据
     */

    /**
     * 1: 营业额
     * 2: 有效订单数
     * 3: 订单完成率
     * 4: 平均客单价
     * 5: 新增用户
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {

        // 查询 有效订单数
        Integer count = orderMapper.validOrders(beginTime, endTime);

        // 查询营业额
        Double allMoney = orderMapper.turnover(beginTime, endTime);
        allMoney = allMoney == null ? 0.0 : allMoney;

        // 订单完成率  (有效订单数/全部订单数)*100%
        // 查询所有订单数
        Integer allOrders = orderMapper.getAllOrders(beginTime, endTime);
        Double rate = 0.0;
        if (allOrders != 0.0){
             rate = (count * 1.0) / allOrders;
        }

        //平均客单价 (营业额/有效订单数)
        Double avg = 0.0;
        if (count!=0.0){
            avg = allMoney / count;
        }
        // 新增用户 较昨日用户数量比较 先查昨日员工数
        Integer user = userMapper.getUser(beginTime, endTime);

        BusinessDataVO businessDataVO = new BusinessDataVO();
        businessDataVO.setNewUsers(user);
        businessDataVO.setTurnover(allMoney);
        businessDataVO.setUnitPrice(avg);
        businessDataVO.setOrderCompletionRate(rate);
        businessDataVO.setValidOrderCount(count);
        return businessDataVO;
    }


    /*
       // 查询套餐总概览
     */
    @Override
    public SetmealOverViewVO getSetmeals(LocalDateTime beginTime, LocalDateTime endTime) {

        // 查询已售数量sold  已停售数量discontinued
        Integer countStart = orderMapper.getStart(beginTime, endTime);
        Integer countStop = orderMapper.getStop(beginTime, endTime);

        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setDiscontinued(countStop);
        setmealOverViewVO.setSold(countStart);

        return setmealOverViewVO;
    }

    /*
      查看菜品总概览
     */
    @Override
    public SetmealOverViewVO getOrders() {
        // 查询已售数量sold  已停售数量discontinued
        Integer countOrdersStart = orderMapper.getOrdersStart();
        Integer countOrdersStop = orderMapper.getOrdersStop();

        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setDiscontinued(countOrdersStop);
        setmealOverViewVO.setSold(countOrdersStart);

        return setmealOverViewVO;
    }


    /*
      查询订单管理数据
      订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     */
    @Override
    public OrderOverViewVO getByOrders(LocalDateTime beginTime, LocalDateTime endTime) {
        HashMap map = new HashMap<>();
        map.put("begin",beginTime);
        map.put("end",endTime);
        map.put("status",Orders.TO_BE_CONFIRMED);

         // 待接单数量
        Integer waiting = orderMapper.getByStatus(map);
         //  待派送
        map.put("status",Orders.CONFIRMED);
        Integer delivered = orderMapper.getByStatus(map);
         //  已完成
        map.put("status",Orders.COMPLETED);
        Integer completed = orderMapper.getByStatus(map);
         // 已取消
        map.put("status",Orders.CANCELLED);
        Integer cancelled = orderMapper.getByStatus(map);
         // 全部订单
        map.put("status",Orders.CANCELLED);
        Integer all = orderMapper.getByStatus(map);
        OrderOverViewVO orderOverViewVO =  OrderOverViewVO.builder()
                .allOrders(all)
                .waitingOrders(waiting)
                .deliveredOrders(delivered)
                .completedOrders(completed)
                .cancelledOrders(cancelled)
                .build();

        return orderOverViewVO;
    }
}
